class Promise {
    constructor(excutor) {
        this.PromiseState = 'pending';
        this.PromiseResult = undefined;

        // 把里面所有的异步执行函数体都存起来 - 存到一个属性里-存多个 - 数组
        this.callBacks = [
            // {
            //     resolve:fn,
            //     reject:fn
            // },
            // {
            //     resolve:fn,
            //     reject:fn
            // },
            // {
            //     resolve:fn,
            //     reject:fn
            // }
        ];
        // 定义
        let resolveFn = (value) => {

            if (this.PromiseState !== 'pending') {
                return;
            }

            this.PromiseState = 'fulfilled';
            this.PromiseResult = value;

            this.callBacks.forEach(item => {
                item.resolve(value)
            })
        }

        let rejectFn = (reason) => {

            if (this.PromiseState !== 'pending') {
                return;
            }

            this.PromiseState = 'rejected';
            this.PromiseResult = reason;

            // if(this.callBacks.length){
            this.callBacks.forEach(item => {
                item.reject(reason);
            })
            // }
        }

        try {
            excutor(resolveFn, rejectFn)
        } catch (error) {
            rejectFn(error)
        }
    }

    // 原型下方法
    then(onResolve, onReject) {
        return new Promise((res, rej) => {

            if (typeof onResolve !== 'function') {
                onResolve = val => val;
            }

            if (typeof onReject !== 'function') {
                onReject = rea => { throw rea };
            }

            let handle = (fnName) => {
                // 模拟then回调的异步执行
                setTimeout(() => {
                    try {
                        let result = fnName(this.PromiseResult)
                        if (result instanceof Promise) {
                            // promise情况
                            result.then(val => {
                                res(val)
                            }, rea => {
                                rej(rea)
                            })
                        } else {
                            // 非promise情况
                            res(result)
                        }
                    } catch (error) {
                        rej(error)
                    }
                }, 0);
            }
            // 同步修改状态时的调用
            if (this.PromiseState === 'fulfilled') {
                handle(onResolve)
            }
            if (this.PromiseState === 'rejected') {
                handle(onReject)
            }
            // 异步修改状态存储函数
            if (this.PromiseState === 'pending') {
                this.callBacks.push({
                    resolve: () => {
                        handle(onResolve)
                    },
                    reject: () => {
                        handle(onReject)
                    }
                })
            }
        });
    }

    catch(onReject) {
        return this.then(undefined, onReject)
    }

    static resolve(val) {
        // console.log(val);
        // 判断参数是不是promise对象
        return new Promise((res, rej) => {
            if (val instanceof Promise) {
                val.then(val => {
                    res(val)
                }, rea => {
                    rej(rea)
                })
            } else {
                res(val)
            }
        })
    }

    static reject(val) {
        return new Promise((res, rej) => {
            rej(val)
        })
    }
}